package com.example.sherlockzhong.plush_v2.View;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

/**
 * Created by SherlockZhong on 9/25/14.
 */
public class CollectionHorizontalScrollView extends HorizontalScrollView {

    public int childrenLength = 0;
    public int windowWidth = 0;
    public int windowHeight = 0;
    public int currentPos = 0;
    private CollectionHorizontalScrollView self = this;

    public CollectionHorizontalScrollView(Context context) {
        super(context);
    }

    public CollectionHorizontalScrollView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public void setParams(int length, int width, int height) {
        this.childrenLength = length;
        this.windowWidth = width;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        collectionTouchEventHandler(ev);
        return super.onTouchEvent(ev);
    }

    private void collectionTouchEventHandler(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_UP:
                detectScroll();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            default:
                break;
        }
    }

    private void detectScroll() {
        final int scrollX = getScrollX();
        this.post(new Runnable() {
            @Override
            public void run() {
                if (scrollX >= currentPos * windowWidth + windowWidth / 3) {
                    currentPos++;
                } else if (scrollX <= currentPos * windowWidth - windowWidth / 3) {
                    currentPos--;
                }
                self.smoothScrollTo(currentPos * windowWidth, 0);
            }
        });
    }
}
